import dayjs from 'dayjs';
/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat = 'YYYY-MM-DD') {
  if (!time) return '';
  const m = dayjs(time);
  return m.isValid() ? m.format(cFormat) : time;
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (`${time}`.length === 10) {
    time = parseInt(time) * 1000;
  } else {
    time = Number(time);
  }
  const d = new Date(time);
  const now = Date.now();

  const diff = (now - d) / 1000;

  if (diff < 30) {
    return '刚刚';
  }
  if (diff < 3600) {
    // less 1 hour
    return `${Math.ceil(diff / 60)}分钟前`;
  }
  if (diff < 3600 * 24) {
    return `${Math.ceil(diff / 3600)}小时前`;
  }
  if (diff < 3600 * 24 * 2) {
    return '1天前';
  }
  if (option) {
    return parseTime(time, option);
  }
  return `${d.getMonth() + 1}月${d.getDate()}日${d.getHours()}时${d.getMinutes()}分`;
}

// 根据传入的年月日获取到某一天00：00
export function getPointDay(year, month, day) {
  return new Date(year, month, day);
}

// 传入时间戳格式日期，返回对应年日月
export const getYearMonthDay = (time = new Date()) => {
  const base = new Date(time);
  return {
    y: base.getFullYear(),
    m: base.getMonth() + 1,
    d: base.getDate()
  };
};

// 比较两个日期年月是否相同
export const isSameYM = (compareDay, baseDay = new Date()) => {
  const { y: y1, m: m1 } = getYearMonthDay(compareDay);
  const { y: y2, m: m2 } = getYearMonthDay(baseDay);
  return y1 === y2 && m1 === m2;
};

// 比较两个日期年月日是否相同
export const isSameYMD = (compareDay, baseDay = new Date()) => {
  const { y: y1, m: m1, d: d1 } = getYearMonthDay(compareDay);
  const { y: y2, m: m2, d: d2 } = getYearMonthDay(baseDay);
  return y1 === y2 && m1 === m2 && d1 === d2;
};

// 获取当前时间的前几天
export function getLastDay(day, compareTime = new Date()) {
  const nowTime = new Date(compareTime).getTime();
  const array = [parseTime(nowTime - (day - 1) * 24 * 3600 * 1000, 'YYYY-MM-DD'), parseTime(nowTime, 'YYYY-MM-DD')];
  return array;
}

// 返回某日期n天后的日期
export function getDay(day, today = new Date()) {
  const targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds); // 注意，这行是关键代码
  const tYear = today.getFullYear();
  let tMonth = today.getMonth();
  let tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return `${tYear}-${tMonth}-${tDate}`;
}

// 计算两日期之间差值
export function getDateDiff(startTime = new Date(), endTime = new Date()) {
  const remainingTimeStamp = endTime.getTime() - startTime.getTime();

  const days = Math.floor(remainingTimeStamp / 1000 / 60 / 60 / 24);
  const hours = Math.floor((remainingTimeStamp / 1000 / 60 / 60) % 24);
  const minutes = Math.floor((remainingTimeStamp / 1000 / 60) % 60);
  const seconds = Math.floor((remainingTimeStamp / 1000) % 60);
  return {
    days: `${days < 10 ? '0' : ''}${days}`,
    hours: `${hours < 10 ? '0' : ''}${hours}`,
    minutes: `${minutes < 10 ? '0' : ''}${minutes}`,
    seconds: `${seconds < 10 ? '0' : ''}${seconds}`
  };
}

function doHandleMonth(month) {
  let m = month;
  if (month.toString().length === 1) {
    m = `0${month}`;
  }
  return m;
}

export function durationTrans(duration) {
  let b = '';
  let h = parseInt(duration / 3600);
  let m = parseInt((duration % 3600) / 60);
  let s = parseInt((duration % 3600) % 60);
  if (h > 0) {
    h = h < 10 ? `0${h}` : h;
    b += `${h}:`;
  }
  m = m < 10 ? `0${m}` : m;
  s = s < 10 ? `0${s}` : s;
  b += `${m}:${s}`;
  return b;
}

export function formatStayTime(time) {
  let unit = '';
  let stayTime = '';
  if (time >= 3600000) {
    unit = 'h';
    stayTime = time / 3600000;
  } else if (time >= 60000) {
    unit = 'min';
    stayTime = time / 60000;
  } else {
    unit = 's';
    stayTime = time / 1000;
  }
  return `${stayTime.toFixed(2)} ${unit}`;
}
